package com.fenghongzhang.day002;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private TextView tv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);

        tv2 = (TextView) findViewById(R.id.tv2);

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, tv);

                //加载布局
                popupMenu.inflate(R.menu.menu);

                //点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int itemId = item.getItemId();
                        switch (itemId){
                            case R.id.red:
                                tv.setTextColor(Color.parseColor("#ff0000"));
                                break;
                            case R.id.greed:
                                tv.setTextColor(Color.GREEN);
                                break;
                        }
                        return false;
                    }
                });

                popupMenu.show();
            }
        });

        //长按的效果
        registerForContextMenu(tv);
    }



    //加载菜单文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //上下文菜单.
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                tv.setTextColor(Color.parseColor("#ff0000"));

                break;
            case R.id.greed:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                tv.setTextColor(Color.GREEN);
                break;
        }
        return super.onContextItemSelected(item);
    }

    //点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.greed:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void btn(View view) {

        final PopupWindow popupWindow = new PopupWindow(this);

        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);

        TextView textView = inflate.findViewById(R.id.pop_tv1);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        //修改窗口透明度
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getWindow().setAttributes(attributes);

        //消失监听器
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;
                getWindow().setAttributes(attributes);
            }
        });

        //1,布局
        popupWindow.setContentView(inflate);
        //2,宽
        popupWindow.setWidth(400);
        //3,高
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //可以取消
        popupWindow.setOutsideTouchable(true);


        //动画
        popupWindow.setAnimationStyle(R.style.pop);


        //显示在哪里
//        popupWindow.showAsDropDown(tv,100,100);
        View inflate1 = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);




    }
}
